﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;

namespace MG1
{
    public class InputManager
    {
        float numPadConst1 = 0.02f;
        float numPadConst2 = 0.1f;

        GamePadState oldPad;
        GamePadState newPad;
        KeyboardState oldKey;
        KeyboardState newKey;
        MouseState newMouse;
        MouseState oldMouse;

        public bool upPressed, downPressed, leftPressed, rightPressed, upKeyDown, downKeyDown, leftKeyDown, rightKeyDown;
        public bool acceptPressed, escapePressed, cancelPressed, acceptKeyDown, escapeKeyDown, cancelKeyDown;
        public bool action1Pressed, action2Pressed, action3Pressed, action4Pressed, action1KeyDown, action2KeyDown, action3KeyDown, action4KeyDown;

        public bool mouseWithinViewport;
        public bool mouseLeftPressed, mouseRightPressed, mouseLeftKeyDown, mouseRightKeyDown;


        public InputManager()
        {
            UpdateNewButtonState();
            UpdateOldButtonState();
            NullifyKeys();
        }

        public void UpdateNewButtonState()
        {
            newKey = Keyboard.GetState();
            newPad = GamePad.GetState(PlayerIndex.One);
            newMouse = Mouse.GetState();
        }
        public void UpdateOldButtonState()
        {
            oldPad = newPad;
            oldKey = newKey;
            oldMouse = newMouse;
        }
        public void Update()
        {
            upPressed = UpPressed();
            downPressed = DownPressed();
            leftPressed = LeftPressed();
            rightPressed = RightPressed();
            upKeyDown = UpKeyDown();
            downKeyDown = DownKeyDown();
            leftKeyDown = LeftKeyDown();
            rightKeyDown = RightKeyDown();


            acceptPressed = AcceptPressed();
            escapePressed = EscapePressed();
            cancelPressed = CancelPressed();
            acceptKeyDown = AcceptKeyDown();
            escapeKeyDown = EscapeKeyDown();
            cancelKeyDown = CancelKeyDown();

            action1Pressed = Action1Pressed();
            action2Pressed = Action2Pressed();
            action3Pressed = Action3Pressed();
            action4Pressed = Action4Pressed();
            action1KeyDown = Action1KeyDown();
            action2KeyDown = Action2KeyDown();
            action3KeyDown = Action3KeyDown();
            action4KeyDown = Action4KeyDown();

            mouseWithinViewport = MouseWithinViewport();
            if (mouseWithinViewport)
            {
                mouseLeftPressed = MouseLeftPressed();
                mouseRightPressed = MouseRightPressed();
                mouseLeftKeyDown = MouseLeftKeyDown();
                mouseRightKeyDown = MouseRightKeyDown();
            }
            else
            {
                mouseLeftPressed = false;
                mouseRightPressed = false;
                mouseLeftKeyDown = false;
                mouseRightKeyDown = false;
            }
        }
        public void NullifyKeys()
        {
            upPressed = false;
            downPressed = false;
            leftPressed = false;
            rightPressed = false;
            upKeyDown = false;
            downKeyDown = false;
            leftKeyDown = false;
            rightKeyDown = false;

            acceptPressed = false;
            escapePressed = false;
            cancelPressed = false;
            acceptKeyDown = false;
            escapeKeyDown = false;
            cancelKeyDown = false;

            action1Pressed = false;
            action2Pressed = false;
            action3Pressed = false;
            action4Pressed = false;
            action1KeyDown = false;
            action2KeyDown = false;
            action3KeyDown = false;
            action4KeyDown = false;

            mouseWithinViewport = false;
        }



        private bool UpPressed()
        {
            if (oldKey.IsKeyDown(Keys.W) && !newKey.IsKeyDown(Keys.W))
                return true;
            else if (oldKey.IsKeyDown(Keys.Up) && !newKey.IsKeyDown(Keys.Up))
                return true;
            else if (oldPad.ThumbSticks.Left.Y > numPadConst1 && newPad.ThumbSticks.Left.Y <= numPadConst1)
                return true;
            return false;
        }
        private bool DownPressed()
        {
            if (oldKey.IsKeyDown(Keys.S) && !newKey.IsKeyDown(Keys.S))
                return true;
            else if (oldKey.IsKeyDown(Keys.Down) && !newKey.IsKeyDown(Keys.Down))
                return true;
            else if (oldPad.ThumbSticks.Left.Y < -numPadConst1 && newPad.ThumbSticks.Left.Y >= -numPadConst1)
                return true;
            return false;
        }
        private bool LeftPressed()
        {
            if (oldKey.IsKeyDown(Keys.A) && !newKey.IsKeyDown(Keys.A))
                return true;
            else if (oldKey.IsKeyDown(Keys.Left) && !newKey.IsKeyDown(Keys.Left))
                return true;
            else if (oldPad.ThumbSticks.Left.X < -numPadConst1 && newPad.ThumbSticks.Left.X >= -numPadConst1)
                return true;
            return false;
        }
        private bool RightPressed()
        {
            if (oldKey.IsKeyDown(Keys.D) && !newKey.IsKeyDown(Keys.D))
                return true;
            else if (oldKey.IsKeyDown(Keys.Right) && !newKey.IsKeyDown(Keys.Right))
                return true;
            else if (oldPad.ThumbSticks.Left.X > numPadConst1 && newPad.ThumbSticks.Left.X <= numPadConst1)
                return true;
            return false;
        }
        private bool UpKeyDown()
        {
            if (newKey.IsKeyDown(Keys.W) || newKey.IsKeyDown(Keys.Up) || newPad.ThumbSticks.Left.Y > numPadConst1)
                return true;
            return false;
        }
        private bool DownKeyDown()
        {
            if (newKey.IsKeyDown(Keys.S) || newKey.IsKeyDown(Keys.Down) || newPad.ThumbSticks.Left.Y < -numPadConst1)
                return true;
            return false;
        }
        private bool LeftKeyDown()
        {
            if (newKey.IsKeyDown(Keys.A) || newKey.IsKeyDown(Keys.Left) || newPad.ThumbSticks.Left.X < -numPadConst1)
                return true;
            return false;
        }
        private bool RightKeyDown()
        {
            if (newKey.IsKeyDown(Keys.D) || newKey.IsKeyDown(Keys.Right) || newPad.ThumbSticks.Left.X > numPadConst1)
                return true;
            return false;
        }

        private bool AcceptPressed()
        {
            if (oldKey.IsKeyDown(Keys.Space) && !newKey.IsKeyDown(Keys.Space))
                return true;
            else if (oldKey.IsKeyDown(Keys.Enter) && !newKey.IsKeyDown(Keys.Enter))
                return true;
            else if (oldPad.IsButtonDown(Buttons.A) && !newPad.IsButtonDown(Buttons.A))
                return true;
            else if (oldPad.IsButtonDown(Buttons.Start) && !newPad.IsButtonDown(Buttons.Start))
                return true;
            return false;
        }
        private bool EscapePressed()
        {
            if (oldKey.IsKeyDown(Keys.Escape) && !newKey.IsKeyDown(Keys.Escape))
                return true;
            else if (oldPad.IsButtonDown(Buttons.Back) && !newPad.IsButtonDown(Buttons.Back))
                return true;
            return false;
        }
        private bool CancelPressed()
        {
            if (oldKey.IsKeyDown(Keys.Escape) && !newKey.IsKeyDown(Keys.Escape))
                return true;
            else if (oldPad.IsButtonDown(Buttons.Back) && !newPad.IsButtonDown(Buttons.Back))
                return true;
            else if (oldPad.IsButtonDown(Buttons.B) && !newPad.IsButtonDown(Buttons.B))
                return true;
            return false;
        }
        private bool AcceptKeyDown()
        {
            if (newKey.IsKeyDown(Keys.Space) || newKey.IsKeyDown(Keys.Enter) || newPad.IsButtonDown(Buttons.A) || newPad.IsButtonDown(Buttons.Start))
                return true;
            return false;
        }
        private bool EscapeKeyDown()
        {
            if (newKey.IsKeyDown(Keys.Space) || newPad.IsButtonDown(Buttons.Back))
                return true;
            return false;
        }
        private bool CancelKeyDown()
        {
            if (newKey.IsKeyDown(Keys.Space) || newPad.IsButtonDown(Buttons.Back) || newPad.IsButtonDown(Buttons.B))
                return true;
            return false;
        }

        private bool Action1Pressed()
        {
            if (oldMouse.LeftButton == ButtonState.Pressed && newMouse.LeftButton == ButtonState.Released)
                return true;
            else if (oldPad.IsButtonDown(Buttons.A) && !newPad.IsButtonDown(Buttons.A))
                return true;
            return false;
        }
        private bool Action2Pressed()
        {
            if (oldMouse.RightButton == ButtonState.Pressed && newMouse.RightButton == ButtonState.Released)
                return true;
            else if (oldPad.IsButtonDown(Buttons.B) && !newPad.IsButtonDown(Buttons.B))
                return true;
            return false;
        }
        private bool Action3Pressed()
        {
            if (oldKey.IsKeyDown(Keys.Space) && !newKey.IsKeyDown(Keys.Space))
                return true;
            else if (oldPad.IsButtonDown(Buttons.X) && !newPad.IsButtonDown(Buttons.X))
                return true;
            return false;
        }
        private bool Action4Pressed()
        {
            if (oldKey.IsKeyDown(Keys.E) && !newKey.IsKeyDown(Keys.E))
                return true;
            else if (oldPad.IsButtonDown(Buttons.Y) && !newPad.IsButtonDown(Buttons.Y))
                return true;
            return false;
        }
        private bool Action1KeyDown()
        {
            if (oldMouse.LeftButton == ButtonState.Pressed || oldPad.IsButtonDown(Buttons.A))
                return true;
            return false;
        }
        private bool Action2KeyDown()
        {
            if (oldMouse.RightButton == ButtonState.Pressed || oldPad.IsButtonDown(Buttons.B))
                return true;
            return false;
        }
        private bool Action3KeyDown()
        {
            if (newKey.IsKeyDown(Keys.Space) || oldPad.IsButtonDown(Buttons.X))
                return true;
            return false;
        }
        private bool Action4KeyDown()
        {
            if (newKey.IsKeyDown(Keys.E) || oldPad.IsButtonDown(Buttons.Y))
                return true;
            return false;
        }

        private bool MouseLeftPressed()
        {
            if (oldMouse.LeftButton == ButtonState.Pressed && newMouse.LeftButton == ButtonState.Released)
                return true;
            return false;
        }
        private bool MouseRightPressed()
        {
            if (oldMouse.RightButton == ButtonState.Pressed && newMouse.RightButton == ButtonState.Released)
                return true;
            return false;
        }
        private bool MouseLeftKeyDown()
        {
            if (newMouse.LeftButton == ButtonState.Pressed)
                return true;
            return false;
        }
        private bool MouseRightKeyDown()
        {
            if (newMouse.RightButton == ButtonState.Pressed)
                return true;
            return false;
        }

        private bool MouseWithinViewport()
        {
            if(newMouse.Position.X < 0 || 
               newMouse.Position.Y < 0 || 
               newMouse.Position.X > G.graph.GraphicsDevice.Viewport.Width || 
               newMouse.Position.Y > G.graph.GraphicsDevice.Viewport.Height)
                return false;
            return true;
        }
        public Point MousePosition()
        {
            return newMouse.Position;
        }
        public Vector2 MousePosition2()
        {
            return new Vector2(newMouse.Position.X, newMouse.Position.Y);
        }

    }
}
